import Vue from 'vue'
import Vuex from 'vuex'

import createPersistedState from 'vuex-persistedstate'
// import {
//   api
// } from '@/utils/http'
import {
  getUserInfo
} from '@/utils/wx'
Vue.use(Vuex)
let store = new Vuex.Store({
  state: {
    // 目前的switchtab指向和之前的
    tab_: 0,
    prev_tab_: -1,
    // 跳转限制开关
    mark: true,
    info: '',
    // 未读消息的数量
    unread_news: 2,
    userInfo: '',
    // 全局响应信息
    toModal (msg, status) {
      return new Promise((resolve, reject) => {
        wx.showToast({
          title: msg,
          mask: true,
          duration: 1500,
          icon: status || 'none',
          success (res) {
            setTimeout(() => {
              resolve(res)
            }, 1500)
          },
          fail (e) {
            reject(e)
          }
        })
      })
    },
    // tabbar
    tabList: [{
      pagePath: '/pages/one/main',
      text: '首页',
      iconPath: '/static/images/nav1.png',
      selectedIconPath: '/static/images/nav1_.png'
    },
    {
      pagePath: '/pages/urgent/main',
      text: '加急',
      iconPath: '/static/images/nav2.png',
      selectedIconPath: '/static/images/nav2_.png'
    },
    {
      pagePath: '/pages/news/main',
      text: '消息',
      iconPath: '/static/images/nav3.png',
      selectedIconPath: '/static/images/nav3_.png'
    },
    {
      pagePath: '/pages/my/main',
      text: '我的',
      iconPath: '/static/images/nav4.png',
      selectedIconPath: '/static/images/nav4_.png'
    }
    ]
  },

  mutations: {
    updateTab: (state, tab) => {
      console.log(state.tab_ + '----123')
      if (!state.mark) return false
      state.mark = false
      var url = state.tabList[tab].pagePath
      state.prev_tab_ = state.tab_
      state.tab_ = tab
      wx.switchTab({
        url
      })
    },
    // 写到actions
    urlBack: state => {
      state.mark = true
    },
    // 保存信息
    saveInfo (state, info) {
      state.info = info
    },
    // 保留用户信息
    createUser (state, userInfo) {
      state.userInfo = userInfo
    }
  },
  actions: {
    async urlBack ({
      dispatch,
      commit
    }, tab) {
      await dispatch('updateTab', tab)
      setTimeout(() => {
        commit('urlBack')
      }, 500)
    },
    async updateTab ({
      commit
    }, tab) {
      commit('updateTab', tab)
    },
    // 时间戳转换
    dateFormat (state, {
      date,
      pattern = 'YYYY/MM/DD HH:mm:ss',
      moment
    }) {
      return new Promise((resolve, reject) => {
        const time = moment(date).format(pattern)
        resolve(time)
      })
    },
    // 获取用户信息
    async getUserInfo ({
      commit
    }) {
      const {
        userInfo
      } = await getUserInfo({
        withCredenitals: false
      })
      userInfo.avatarUrl = userInfo.avatarUrl
      userInfo.nickName = userInfo.nickName
      userInfo.userId = encodeURIComponent(userInfo.nickName + userInfo.city + userInfo.gender + userInfo.country)
      commit('createUser', userInfo)
      return userInfo
    }
  },
  getters: {

  },
  plugins: [
    // vuex-persistedstate，使vuex状态持久化（缓存到本地）
    createPersistedState({
      storage: {
        getItem: key => wx.getStorageSync(key),
        setItem: (key, value) => wx.setStorageSync(key, value),
        removeItem: key => wx.removeStorageSync(key)
      }
    })
  ]
})

export default store
